package com.aitoedu.server.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.aitoedu.server.constant.ModuleEnum;
import com.aitoedu.server.dto.CreateConversationDTO;
import com.aitoedu.server.entity.Conversation;
import com.aitoedu.server.entity.Title;
import com.aitoedu.server.mapper.ConversationMapper;
import com.aitoedu.server.mapper.TitleMapper;
import com.aitoedu.server.service.TitleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
@Slf4j
public class TitleServiceImpl extends ServiceImpl<TitleMapper, Title>
    implements TitleService {

    @Autowired
    private TitleMapper titleMapper;

    @Autowired
    private ConversationMapper conversationMapper;
    @Override
    public String createConversation(CreateConversationDTO createConversationDTO) {
        Title title = Title.builder()
                .id(createConversationDTO.getTitleId())
                .title(createConversationDTO.getTitle())
                .createTime(LocalDateTime.now())
                .save(0)
                .isDeleted(1)
                .sid(Long.valueOf((String) StpUtil.getLoginId()))
                .build();
        String content = "你好，这里是"+ ModuleEnum.valueOfNum(createConversationDTO.getModuleId()).getModuleName()+
                "板块，我是智教助手，利用AI技术为您提供专业、高效的教育服务。请问有什么学习需求，我可以帮您解答或协助解决吗";
        Conversation conversation = Conversation.builder()
                .titleId(title.getId())
                .createTime(LocalDateTime.now())
                .role(0)
                .content(content)
                .build();
        conversationMapper.insert(conversation);
        titleMapper.insert(title);
        return content;
    }

    @Override
    public void deleteConversation(Long titleId) {
        titleMapper.deleteById(titleId);
        conversationMapper.delete(new QueryWrapper<Conversation>()
                .eq("title_id",titleId)
        );
    }
}
